import {
	defineStore
} from 'pinia';

import {
	getDictionaryList
} from '@/api/permission';

export const useDictionaryStore = defineStore('dictionary', {
	state: () => ({
		dictionaryList: [],
	}),
	getters: {},
	actions: {
		async getDictionaryList() {
			const res = await getDictionaryList();
			this.dictionaryList = res || [];
		},
		get(key, type = 'string') {
			const result = [];
			this.dictionaryList.forEach((item) => {
				if (item.dictType === key) {
					const value = {
						string: String(item.value),
						number: Number(item.value)
					} [type];
					result.push({
						...item,
						value
					});
				}
			});
			return result;
		},
		analysis(key, value) {
			value = value?.toString();
			if (!value) return '';
			const list = this.get(key);
			return list.find((item) => item.value === value)?.label || '';
		},
	},
});